package com.olddriver.crm.web.controller;

import com.olddriver.crm.annotation.PermissionName;
import com.olddriver.crm.domain.Carproduct;
import com.olddriver.crm.page.PageResult;
import com.olddriver.crm.query.CarproductQueryObject;
import com.olddriver.crm.service.ICarproductService;
import com.olddriver.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("carproduct")
public class CarproductController {
    @Autowired
    private ICarproductService carproductService;


    @RequiresPermissions("carproduct:list")
    @PermissionName("查看机构")
    @RequestMapping("list")
    public String list() {
        return "carproduct/list";
    }

    @RequestMapping("listAll")
    @ResponseBody
    public List<Carproduct> listAll() {
        return carproductService.selectAll();
    }

    @RequestMapping("query")
    @RequiresPermissions("carproduct:query")
    @PermissionName("机构列表")
    @ResponseBody
    public PageResult query(CarproductQueryObject qo) {
        PageResult result = carproductService.query(qo);
        return result;
    }

    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions("carproduct:saveOrUpdate")
    @PermissionName("机构编辑")
    public JsonResult saveOrUpdate(Carproduct carproduct) {
        Long id = carproduct.getId();
        try {
            if (id != null) {
                carproduct.setState(true);
                carproductService.updateByPrimaryKey(carproduct);
            } else {
                carproduct.setState(true);
                carproductService.insert(carproduct);
            }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }


    @ResponseBody
    @RequestMapping("changeState")
    @RequiresPermissions("carproduct:saveOrUpdate")
    @PermissionName("机构编辑")
    public JsonResult changeState(Long id) {
        try {
            if(id !=null){
                carproductService.changeState(id);
            }
            return JsonResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.faliure();
        }
    }

    @ResponseBody
    @RequestMapping("getCarproductByMechId")
    public List<Carproduct> getCarproductByMechId(Long mechId){
        return carproductService.getCarproductByMechId(mechId);
    }


}
